package com.controller;

import com.domain.ChatRoomEntity;
import com.domain.ChatUserRelaEntity;
import com.dto.JsonResponse;
import com.service.ChatUserRelaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 聊天室房间人员表 前端控制器
 * </p>
 *
 * @author sly
 * @since 2023-12-30
 */
@RestController
@RequestMapping("/chatUser")
public class ChatUserRelaController {

    @Autowired
    private ChatUserRelaService chatUserRelaService;

    /**
     * 根据聊天室查询用户列表
     */
    @GetMapping("/getChatUsers")
    public JsonResponse<List<ChatUserRelaEntity>> getChatUsers(@RequestParam Long chatId) {
        List<ChatUserRelaEntity> result = chatUserRelaService.getChatUsers(chatId);
        return new JsonResponse<>(result);
    }

    /**
     * 查询聊天室用户人数
     */
    @GetMapping("/getChatUserCount")
    public JsonResponse<Long> getChatUserCount(@RequestParam Long chatId) {
        Long userCount = chatUserRelaService.getChatUserCount(chatId);
        return new JsonResponse<>(userCount);
    }

    /**
     * 根据用户查询聊天室列表
     */
    @GetMapping("/getUserChats")
    public JsonResponse<List<ChatRoomEntity>> getUserChats(@RequestParam Long userId) {
        List<ChatRoomEntity> result = chatUserRelaService.getUserChats(userId);
        return new JsonResponse<>(result);
    }

    /**
     * 新增聊天室用户
     */
    @PostMapping("/addChatUser")
    public JsonResponse<String> addChatUser(@RequestBody ChatUserRelaEntity chatUserRelaEntity) {
        chatUserRelaService.addChatUser(chatUserRelaEntity);
        return JsonResponse.success();
    }

    /**
     * 修改聊天室用户最后观看时间
     */
    @PostMapping("/updateChatUser")
    public JsonResponse<String> updateChatUser(@RequestBody ChatUserRelaEntity chatUserRelaEntity) {
        chatUserRelaService.updateChatUser(chatUserRelaEntity);
        return JsonResponse.success();
    }

    /**
     * 删除聊天室用户
     */
    @GetMapping("/deleteChatUser")
    public JsonResponse<String> deleteChatUser(@RequestParam ChatUserRelaEntity chatUserRelaEntity) {
        chatUserRelaService.deleteChatUser(chatUserRelaEntity);
        return JsonResponse.success();
    }
}
